%%
%% This is file `highlightlatex.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% highlightlatex.dtx 
%% 
%% IMPORTANT NOTICE:
%% 
%% For the copyright see the source file.
%% 
%% Any modified versions of this file must be renamed
%% with new filenames distinct from highlightlatex.sty.
%% 
%% For distribution of the original source see the terms
%% for copying and modification in the file highlightlatex.dtx.
%% 
%% This generated file may be distributed as long as the
%% original source files, as listed above, are part of the
%% same distribution. (The sources need not necessarily be
%% in the same archive or directory.)
\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{highlightlatex}[2021/03/17 highlightlatex v1.0.0 + Dev]



\RequirePackage{listings}
\RequirePackage{xcolor}

\RequirePackage{xkeyval}
\RequirePackage{etoolbox}

%% Snippet source:
%% https://tex.stackexchange.com/questions/406015/defining-macro-gsetlength-as-global-setlength-reliable
\gdef\gsetlength#1#2{%
\begingroup
\setlength\skip@{#2}%
\global#1=\skip@
\endgroup
}

\def\@highlight@frame{none}
\newlength{\hll@offset@fixskip}
\setlength{\hll@offset@fixskip}{0pt}

\newif\ifhllStyleAnywhere
\newcommand\defaultgobble{0}
\newif\ifhllDebugFrame
\newlength\hllBeforeSkip
\setlength\hllBeforeSkip{2pt}
\newlength\hllAfterSkip
\setlength\hllAfterSkip{0pt}

\DeclareOptionX{frame}{
\setframe{#1}
}

\DeclareOptionX{noframe}{
\setframe{none}
}

\def\setframe#1{%
\def\frameval{#1}%
\ifx\relax#1\relax
\def\frameval{lines}%
\fi
\def\linesvalue{lines}%
\xdef\@highlight@frame{\frameval}%
\ifx\frameval\linesvalue
\gsetlength{\hll@offset@fixskip}{6pt}%
\else
\gsetlength{\hll@offset@fixskip}{0pt}%
\fi
}

\DeclareOptionX{styleanywhere}{
\hllStyleAnywheretrue
}

\DeclareOptionX*{\PackageWarning{highlightlatex}{`\CurrentOption' ignored}}
\ExecuteOptionsX{frame=lines}

\ProcessOptionsX\relax

\colorlet{curlyBrackets}{red!50!blue}
\colorlet{squareBrackets}{blue!50!white}
\colorlet{codeBackground}{gray!10!white}
\colorlet{comment}{green!40!black}

\colorlet{accDefault}{blue!90!black}
\definecolor{accStructure}{RGB}{0,149,255}

\colorlet{accentB}{green!60!black}

\catcode`\{=11
\let\myopenbrace={
\catcode`\{=1

\catcode`\}=11
\let\myclosebrace=}
\catcode`\}=2

\lstdefinelanguage{ColoredLaTeX}
[LaTeX]{TeX}{
backgroundcolor=\color{codeBackground},
keywordstyle=\bfseries\color{accDefault},
basicstyle=\footnotesize\ttfamily,
commentstyle=\color{comment},
numbers=none,
tabsize=2,
mathescape=false,
alsoletter={$@_!|?$},
classoffset=0,
keywordstyle=\color{accDefault},
texcsstyle=*\color{accDefault},
moretexcs={subsection,thesubsection,thesection,theoremstyle},
deletetexcs={begin,end},
classoffset=1,
keywordstyle=\color{accStructure},
texcsstyle=*\color{accStructure},
moretexcs={begin,end},
classoffset=2,
escapeinside=~~,
classoffset=0,
literate={\{}{{\textcolor{curlyBrackets}{\myopenbrace}}}{1}
{\}}{{\textcolor{curlyBrackets}{\myclosebrace}}}{1}
{[}{{\textcolor{squareBrackets}{[}}}{1}
{]}{{\textcolor{squareBrackets}{]}}}{1}%
}
\lstset{language=ColoredLaTeX}

\newcommand{\hllDelimDollar}[1][\color{green!40!black}]{%
\lstset{
classoffset=2,
moredelim=**[s][#1]{$}{$},
classoffset=0
}%
}

\newcommand{\hllDelimParen}[1][\color{green!40!black}]{%
\lstset{
classoffset=2,
moredelim=**[s][#1]{\(}{\)},
classoffset=0
}%
}

\def\hll@assignlabel#1#2{%
\expandafter\gdef\csname hll@labeltoclassoffset@#1\endcsname{#2}%
}

\hll@assignlabel{default}{0}
\hll@assignlabel{structure}{1}

%% =========================
%%   block & inline markup
%% =========================

\newcommand{\hllPrepareBlock}{%
\lstset{framesep=0.1cm,framerule=0.6pt}%
\def\setHighlightFrame##1{%
\lstset{frame=##1}%
}%
\expandafter\setHighlightFrame\expandafter{\@highlight@frame}%
\lstset{belowskip=0pt,aboveskip=0pt,belowcaptionskip=0pt,gobble=\defaultgobble}%
\setlength\parskip{0pt}%
\setlength\parindent{0pt}%
}

\lstnewenvironment{highlightblock}[1][]
{%
\vspace{\hllBeforeSkip}%
\begingroup
\hllPrepareBlock\lstset{#1}%
}{%
\nointerlineskip
\endgroup
\vspace{\hllAfterSkip}%
}

\lstnewenvironment{hllblock}[1][]
{%
\vspace{\hllBeforeSkip}%
\begingroup
\hllPrepareBlock\lstset{#1}%
}{%
\endgroup
\vspace{\hllAfterSkip}%
}

\def\hll{\lstinline}

%% ================
%%   block saving
%% ================

\newenvironment{saveblock}[1]{%
\expandafter\ifx\csname @codebox@#1\endcsname\relax
\expandafter\newbox\csname @codebox@#1\endcsname\relax
\fi
%% The trick from latex/base/latex.ltx
\edef\@bracehack{%
\endgroup
\expandafter\setbox\csname @codebox@#1\endcsname\vbox{%
\begingroup\aftergroup}%
\def\noexpand\@currenvir{\@currenvir}%
\def\noexpand\@currenvline{\on@line}}%
\@bracehack
\begingroup\vspace{\hll@offset@fixskip}
\ignorespaces%
}{%
\endgroup
}

\newcommand\useblock[1]{%
\ifhllDebugFrame
\par
{%
\setlength{\fboxsep}{0pt}%
\fcolorbox{orange}{red}{\expandafter\usebox\csname @codebox@#1\endcsname}%
}%
\par
\else
\par\expandafter\usebox\csname @codebox@#1\endcsname\par
\fi
}

\newcommand\consumeblock[1]{%
\ifhllDebugFrame
\par
{%
\setlength{\fboxsep}{0pt}%
\setlength{\fboxrule}{1pt}%
\fcolorbox{orange}{red}{\leavevmode\expandafter\box\csname @codebox@#1\endcsname\relax}%
}
\par
\expandafter\let\csname @codebox@#1\endcsname\relax
\else
\par\leavevmode\expandafter\box\csname @codebox@#1\endcsname\relax\par
\expandafter\let\csname @codebox@#1\endcsname\relax
\fi
}

%% ===================
%%   updatehighlight
%% ===================

\def\hll@update@label{}

\newcounter{hll@update@state}

\newcounter{hll@update@nextclassoffset}
\setcounter{hll@update@nextclassoffset}{5}

\newcounter{hll@update@classoffset}

\def\hll@update@fixclassoffset{%
\ifnum\value{hll@update@state}<1\relax
\setcounter{hll@update@classoffset}{\thehll@update@nextclassoffset}%
\stepcounter{hll@update@nextclassoffset}%
\setcounter{hll@update@state}{1}%
\fi
}

\newcommand{\updatehighlight}[1]{%
\setcounter{hll@update@state}{-1}%
\gdef\hll@update@label{}%
\setkeys{updatehighlight}{#1}%
\lstset{classoffset=0}%
}

%%% KEY: name
%%% ---------

\define@key{updatehighlight}{name}{%
\hll@update@setlabel{#1}%
}

\def\hll@update@setlabel#1{
\gdef\hll@update@label{#1}%
\expandafter\def\expandafter\@capturedlabel\expandafter{\csname hll@labeltoclassoffset@#1\endcsname}%
\expandafter\unless\expandafter\ifx\@capturedlabel\relax\relax
\edef\@val{\@capturedlabel}%
\setcounter{hll@update@classoffset}{\@val}%
\setcounter{hll@update@state}{1}%
\else
\edef\@val{\thehll@update@nextclassoffset}%
\setcounter{hll@update@classoffset}{\@val}%
\stepcounter{hll@update@nextclassoffset}%
\setcounter{hll@update@state}{1}%
\expandafter\xdef\@capturedlabel{\@val}%
\fi
}

%%% KEY: classoffset
%%% ----------------

\define@key{updatehighlight}{classoffset}{%
\setcounter{hll@update@classoffset}{#1}%
\setcounter{hll@update@state}{1}%
}

%%% KEY: add
%%% --------

\define@key{updatehighlight}{add}{%
\hll@update@add{#1}%
}

\def\hll@update@add#1{%
\hll@update@fixclassoffset
\def\@inv##1{\forcsvlist{\hll@update@additem{##1}}{#1}}%
\expandafter\@inv\expandafter{\the\c@hll@update@classoffset}
\setcounter{hll@update@state}{2}%
}

\def\hll@update@additem#1#2{%
\if\noexpand#2\relax
\edef\keywordpure{\expandafter\@gobble\string#2}%
\def\callwithpure##1{\hll@update@addmacro{#1}{##1}}%
\expandafter\callwithpure\expandafter{\keywordpure}%
\else
\hll@update@addkeyword{#1}{#2}%
\fi
}

\def\hll@update@addmacro#1#2{%
\lstset{
classoffset=0,deletetexcs={#2},
classoffset=1,deletetexcs={#2},
classoffset=#1,moretexcs={#2},
classoffset=0
}%
}

\def\hll@update@addkeyword#1#2{%
\lstset{
classoffset=0,deletekeywords={#2},
classoffset=1,deletekeywords={#2},
classoffset={#1},otherkeywords={#2},morekeywords={#2},
classoffset=0
}%
}

%%% KEY: remove
%%% -----------

\define@key{updatehighlight}{remove}{%
\hll@update@remove{#1}%
}

\def\hll@update@remove#1{%
\hll@update@fixclassoffset
\def\@inv##1{\forcsvlist{\hll@update@removeitem{##1}}{#1}}%
\expandafter\@inv\expandafter{\the\c@hll@update@classoffset}
\setcounter{hll@update@state}{2}%
}

\def\hll@update@removeitem#1#2{%
\if\noexpand#2\relax
\edef\keywordpure{\expandafter\@gobble\string#2}%
\def\callwithpure##1{\hll@update@removemacro{#1}{##1}}%
\expandafter\callwithpure\expandafter{\keywordpure}%
\else
\hll@update@removekeyword{#1}{#2}%
\fi
}

\def\hll@update@removemacro#1#2{%
\lstset{
classoffset={#1},
deletetexcs={#2},
classoffset=0
}%
}

\def\hll@update@removekeyword#1#2{%
\lstset{
classoffset={#1},
deletekeywords={#2},
classoffset=0
}%
}

%%% KEY: style
%%% ----------

\define@key{updatehighlight}{style}{%
\hll@update@fixclassoffset
\unless\ifhllStyleAnywhere
\ifnum\value{hll@update@state}>1\relax
\setcounter{hll@update@state}{0}%
\hll@update@fixclassoffset
\fi
\fi
\expandafter\hll@update@setstyle\expandafter{\the\c@hll@update@classoffset}{#1}%
}

\def\hll@update@setstyle#1#2{%
\lstset{%
classoffset={#1},
keywordstyle={#2},
texcsstyle={*#2},
}%
}

%%% KEY: color
%%% ----------

\define@key{updatehighlight}{color}{%
\hll@update@fixclassoffset
\unless\ifhllStyleAnywhere
\ifnum\value{hll@update@state}>1\relax
\setcounter{hll@update@state}{0}%
\hll@update@fixclassoffset
\fi
\fi
\expandafter\hll@update@setcolor\expandafter{\the\c@hll@update@classoffset}{#1}%
}

\def\hll@update@setcolor#1#2{%
\lstset{%
classoffset={#1},
keywordstyle={\color{#2}},
texcsstyle={*\color{#2}},
}%
}

%%% KEY: clear
%%% ----------

\define@key{updatehighlight}{clear}[]{%
\hll@update@fixclassoffset
\expandafter\hll@update@clear\expandafter{\the\c@hll@update@classoffset}%
}

\def\hll@update@clear#1{%
\lstset{
classoffset={#1},
texcs={},
keywords={},
classoffset=0
}%
}

\endinput
%%
%% End of file `highlightlatex.sty'.
